package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.User;
import service.UserService;
import utils.Page;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/list")
    public String findAll(){
        return "user";
    }
    @RequestMapping("/add")
    public String add(){
        return "add";
    }
    @RequestMapping("/update")
    public String update(){
        return "update";
    }
    @RequestMapping("/delete")
    public String delete(){
        return "delete";
    }
    //登录
    @RequestMapping("/login")
    //登录  自动填充
    public String login(User user) {
        boolean flag = userService.login(user);
        if(flag){
            return "index";
        }
        else{
            return "login";
        }
    }

    @RequestMapping("/toregist")
    public String toregist(){
        return "regist";
    }
    @RequestMapping("/checkUserNameUnique")
    //忽略视图解析器 异步方法
    @ResponseBody
    public String checkUserNameUnique(@RequestParam("name") String name){
        int result = userService.findUserByName(name);
        if(result>0){
            return "false";
        }
        else{
            return "true";
        }
    }

    @RequestMapping("/list/{currentPage}")
    public String list(User user,@PathVariable("currentPage") Integer currentPage,Model model){
        Page page = new Page();
        int totalCount = userService.count(user);
        page.setTotalCount(totalCount);
        page.setCurrentPage(currentPage);
        page.setTotalPage();
        // 计算偏移量
        int offset = (currentPage - 1) * page.getPageSize();
        List<User> users = userService.findAll(user, offset, page.getPageSize());
        // 把对象存储在作用域中
        model.addAttribute("page",page);
        model.addAttribute("users",users);
        model.addAttribute("user",user);
        return "user/list";

    }
    @RequestMapping("/toadd")
    public String toadd(){
        return "user/add";
    }
    @RequestMapping("/addSave")
    public String addSave(User user){
        int result = userService.addUser(user);
        if(result>0){
            return "redirect:/user/list/1";
        }
        else{
            return "error";
        }
    }
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id){
        int result = userService.deleteUserBuId(id);
        if(result>0){
            return "redirect:/user/list/1";
        }
        else{
            return "error";
        }
    }
    @RequestMapping("/toupdate/{id}")
    public String toupdate(@PathVariable("id") Integer id,Model model){
        model.addAttribute("user", userService.findUserById(id));
        return "user/update";
    }
    @RequestMapping("/updateSave")
    public String updateSave(User user){
        int result = userService.updateUser(user);
        if(result>0){
            return "redirect:/user/list/1";
        }
        else{
            return "error";
        }
    }
}
